<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/9
 * Time: 17:16
 */
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
include_once(ROOTDIR.'include/ap.class.php');
include_once(ROOTDIR.'include/sdk.class.php');

if(empty($_SESSION["userinfo"])){die("<script>top.location.href='index.php'</script>");}

//调用接口类
class Keyreply_con extends ap
{
    //查询消息自动回复
    public function index()
    {
        //用户ID
        $userid = $this->userinfo['userid'];

        //关键词自动回复
        //每页显示记录条数
        $pagesize = 5;
        //当前页码
        $page = (int)$_GET['page']? (int)$_GET['page'] : 1;
        $pagenum = ($page - 1) * $pagesize;
        $sort[]='g='.$_GET["g"];
        $sort[]='m='.$_GET["m"];
        $sort[]='a='.$_GET["a"];

        //分页字符串
        $pagestr = $this->termpage('apreply_key',"where userid={$userid}",$sort,$page,$pagesize);

        //关键词回复
        $keyreply = $this->arrdata('*','apreply_key',"where userid={$userid}","id DESC","limit {$pagenum},{$pagesize}");
        foreach ($keyreply as $kk => $vv)
        {
            $keyreply[$kk]['keys'] = explode(' ', $vv['keywords']);
            if($vv['type'] == 1)
            {
                $msg = $this->arrdata('*', 'apfodder', "where msgid={$vv['content']}");
                foreach ($msg as $k => $v) {
                    $msg[$k] = array(
                        'title' => $v['title'],
                        'img_url' => $v['img_url'],
                        'abstract' => $v['abstract'],
                        'content' => $v['content'],
                        'time' => $v['time'],
                    );
                }
                $keyreply[$kk]['msgtext'] = $msg;
                $keyreply[$kk]['msgnum'] = count($msg);
                $keyreply[$kk]['title'] = $msg[0]['title'];
            }
        }
        //echo '<pre>';
        //print_r($keyreply);die;
        //素材列表
        $fodder = $this->arrdata('*','apfodder',"where userid={$userid}");

        $data = array();
        foreach ($fodder as $k => $v)
        {
            $data[$v['msgid']][] = array(
                'id'       => $v['id'],
                'userid'   => $userid,
                'title'    => $v['title'],
                'img_url'  => $v['img_url'],
                'abstract' => $v['abstract'],
                'content'  => $v['content'],
                'time'     => $v['time'],
                'type'     => $v['type'],
            );
        }

        //echo '<pre>';
        //print_r($data);die;

        $this->smarty->assign("basehost",$this->basehost);
        $this->smarty->assign("fodder",$data);
        $this->smarty->assign("pagestr",$pagestr);
        $this->smarty->assign("keyreply",$keyreply);
        $this->smarty->display("keywordReply.tpl");
    }

    //添加关键词自动回复
    public function add()
    {
        //用户ID
        $userid = $this->userinfo['userid'];

        //接收参数
        $into = array(
            'userid'   => $userid,
            'rulename'    => $_POST['rulename'],
            'keywords'    => $_POST['keywords'],
            'content'    => $_POST['content'],
            'type'    => $_POST['type'],
        );

        //print_r($into);die;
        $res = $this->setdata("apreply_key",$into);
        if($res)
        {
            //提示保存成功
            echo json_encode(array('status' => 1));
        }
    }

    //编辑关键词自动回复
    public function edit()
    {
        $id = $_POST['ruleid'];
        //接收参数
        $into = array(
            'rulename'    => $_POST['rulename'],
            'keywords'    => $_POST['keywords'],
            'content'    => $_POST['content'],
            'type'    => $_POST['type'],
        );
        //修改
        $this->updata('apreply_key',$into,"where id={$id}");
        echo json_encode('ok');die;
    }

    //编辑
    public function kpreedit()
    {
        $id = $_POST['id'];

        $info = $this->viewdata("*","apreply_key","where userid={$id}");

        if($info['type'] == 1)
        {
            $this->smarty->assign('keyrep', $info);
        }
        else
        {
            //回复图文广播
            $msg = $this->arrdata('*', 'apfodder', "where msgid={$info['content']}");
            foreach ($msg as $k => $v) {
                $msg[$k] = array(
                    'title' => $v['title'],
                    'img_url' => $v['img_url'],
                    'abstract' => $v['abstract'],
                    'content' => $v['content'],
                    'time' => $v['time'],
                );
            }
            $msgnum = count($msg);

            $this->smarty->assign("keyrep", $msg);
            $this->smarty->assign("keynum", $msgnum);
        }
    }

    //删除关键词自动回复
    public function del()
    {
        $id = $_POST['ruleid'];
        //删除
        $res = $this->deldata('apreply_key',"where id={$id}");

        if($res)
        {
            //提示删除成功
            echo json_encode(array('status' => 1));

        }
    }

}